package com.movie.service.Impl;

import com.movie.beans.SysMenu;
import com.movie.beans.SysMenuRole;
import com.movie.beans.SysUser;
import com.movie.dao.SysMenuDAO;
import com.movie.service.SysMenuRoleService;
import com.movie.service.SysMenuService;
import com.movie.service.SysUserRoleService;
import com.movie.utils.MenuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Description : 菜单业务实现
 * @Author : nlw
 * @Date : 2021/8/27
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Resource
    private SysMenuDAO menuDAO;
    @Autowired
    private SysUserRoleService userRoleService;
    @Autowired
    private SysMenuRoleService menuRoleService;

    @Override
    public Map selectMenuList(SysUser user) {
        if(user.isAdmin()){
            return MenuUtil.initMenu(menuDAO.selectMenuByParentId(0L));
        }
        List<SysMenuRole> menuRoles=menuRoleService.selectMenuByRole(userRoleService.selectRoleByUserId(user.getUserId()));
        List<SysMenu> rootMenus=new LinkedList<>();
        Map<String,Object> map=new HashMap<>();
        for(SysMenuRole item:menuRoles){
            if(menuDAO.selectMenuByMenuId(item.getMenuId()).getParentId().equals(0L)){
                rootMenus.add(menuDAO.selectMenuByMenuId(item.getMenuId()));
            }

        }
        for(SysMenu rootMenu:rootMenus){
            rootMenu.setChild(menuDAO.selectMenuByParentId(rootMenu.getMenuId()));
        }
        return MenuUtil.initMenu(rootMenus);
    }


    @Override
    public List<SysMenu> selectMenuTreeList(SysUser user) {
        if(user.isAdmin()){
            return menuDAO.selectMenuList();
        }
        return null;
    }
}
